/* Includes--------------------------------------------- */
#include "main.h"
#include "stdio.h"
#include "string.h"
#include "systick.h"
#include "oled_i2c.h"
#include "sd.h"
#include "ff.h"
#include "wav.h"
#include "pwm.h"

/* Define----------------------------------------------- */
#define DISK_PATH "1:"
#define FILE_PATH "1:/"

/* Private funtion delaration--------------------------- */
/* Private variables------------------------------------ */
static FATFS fs;
static FIL file;
static FILINFO fno;
static DIR dir;
static FRESULT res;
static UINT br;
static BYTE rbuff[512];
static WORD leftSound[128];
static WORD rightSound[128];

/* External variables----------------------------------- */
/* External functions----------------------------------- */
int main(void) {
  DWORD pos, sn, count;
  char nextFile = 1;
  char str[12];

  DrvPWMInit();

  rcu_periph_clock_enable(RCU_GPIOA);
  gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_7);
  gpio_output_options_set(GPIOA,GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7);

  systick_config();
	oled_init();
	OLED_Fill(0xFF);
	delay_1ms(100);
	OLED_CLS();

  res = f_mount(&fs, DISK_PATH, 0);
  if (res == FR_OK) {
    res = f_opendir(&dir, FILE_PATH);
  }
//  f_mount(&fs, 0, 0);
  while (1) {
    if (nextFile == 1) {
      gpio_bit_toggle(GPIOA, GPIO_PIN_7);   
      res= f_readdir(&dir, &fno);
      if (res == FR_OK) {
        if (strstr(fno.fname, ".WAV")) {
          sprintf(str, "%s%s", FILE_PATH, (const char *)fno.fname);
          res = f_open(&file, (const TCHAR *)str, FA_READ | FA_OPEN_EXISTING | FA_OPEN_ALWAYS);
          if (res == FR_OK) {
            res = f_read(&file, rbuff, sizeof(rbuff), &br);
            if (res == FR_OK) {
              nextFile = 0;
              pos = WavGetFileHeader(rbuff, br);
              if (pos) {
                OLED_ShowStr(0, 0, (uint8_t *)fno.fname, 2);
                OLED_ShowStr(0, 2, (uint8_t *)"CH:", 2);
                OLED_ShowNum(20, 2, WavGetChannelNum(), 10, 16);
                OLED_ShowStr(0, 4, (uint8_t *)"SR:", 2);
                OLED_ShowNum(20, 4, WavGetSampleRate(), 10, 16);
                OLED_ShowStr(0, 6, (uint8_t *)"BR:", 2);
                OLED_ShowNum(20, 6, WavGetBitRate(), 10, 16);
      //          br -= s;
      //          s = WavGetLeftSound(rbuff, br, leftSound);
      //          s = WavGetRightSound(rbuff, br, rightSound);
              }
            }
          }
        }
      }
//    f_close(&file);
    } else {
      f_lseek(&file, pos);
      res = f_read(&file, rbuff, sizeof(rbuff), &br);
      if (res == FR_OK) {
        count = 0;
        sn = WavGetLeftSound(rbuff, br, leftSound);
        while (count++ < sn) {
          DrvPWMRefresh(leftSound[count]);
//          delay_1ms(1);
        }
        pos += br;
        if (pos == fno.fsize) {
          nextFile = 1;
        }
      }
    }
  }
}

/* Private functions------------------------------------ */
/* End of file------------------------------------------ */
